﻿#include"Gamestart.h"
#include"Widget.h"
#include"Button.h"

IMAGE GameBeginPictures;
IMAGE GameStartPictures;
IMAGE	GameChartsPictures;
IMAGE	GameSetPictures;
IMAGE	GameEndPictures;
IMAGE	PagesNull;
Widget Game_Begin(GAMEWIDTH, GAMEHEIGHT, &GameBeginPictures, Pages::GAMEBEGIN);
Widget Game_Start(GAMEWIDTH, GAMEHEIGHT, &GameStartPictures, Pages::GAMESTART);
Widget Game_Charts(GAMEWIDTH, GAMEHEIGHT, &GameChartsPictures, Pages::GAMECHARTS);
Widget Game_Set(GAMEWIDTH, GAMEHEIGHT, &GameSetPictures, Pages::GAMESET);
Widget Game_End(GAMEWIDTH, GAMEHEIGHT, &GameEndPictures, Pages::GAMEEND);
Widget Pages_Null(GAMEWIDTH, GAMEHEIGHT, &PagesNull, Pages::PAGESNULL);

Gamestart::Gamestart() {
	//加载游戏图片
	loadimage(&GameBeginPictures, "飞机大战素材/背景素材/图片6.jpg", GAMEWIDTH, GAMEHEIGHT);
	widgets[Pages::GAMEBEGIN] = &Game_Begin;


	loadimage(&GameStartPictures, "飞机大战素材/背景素材/图片1.jpg", GAMEWIDTH, GAMEHEIGHT);
	widgets[Pages::GAMESTART] = &Game_Start;



	loadimage(&GameChartsPictures, "飞机大战素材/背景素材/图片10.jpg", GAMEWIDTH, GAMEHEIGHT);
	widgets[Pages::GAMECHARTS] = &Game_Charts;


	loadimage(&GameSetPictures, "飞机大战素材/背景素材/图片9.jpg", GAMEWIDTH, GAMEHEIGHT);
	widgets[Pages::GAMESET] = &Game_Set;


	loadimage(&GameEndPictures, "飞机大战素材/背景素材/图片7.jpg", GAMEWIDTH, GAMEHEIGHT);
	widgets[Pages::GAMEEND] = &Game_End;


	loadimage(&PagesNull, "飞机大战素材/背景素材/图片4.jpg", GAMEWIDTH, GAMEHEIGHT);
	widgets[Pages::PAGESNULL] = &Pages_Null;

	//接收鼠标消息
	peekmessage(&msg, EX_MOUSE | EX_KEY);

	//创建游戏类
	game = new Game();

}
#define FILENAME "Game.txt"

void Gamestart::gamestarthere()
{

	Button* start = new Button(125, 100, 150, 50, "开始游戏", Pages::GAMESTART);
	Button* set = new Button(125, 200, 150, 50, "音乐设置", Pages::GAMESET);
	Button* end = new Button(125, 400, 150, 50, "结束游戏", Pages::GAMEEND);
	Button* charts = new Button(125, 300, 150, 50, "积分排行", Pages::GAMECHARTS);
	widgets[Pages::GAMEBEGIN]->addbutton(start);
	widgets[Pages::GAMEBEGIN]->addbutton(set);
	widgets[Pages::GAMEBEGIN]->addbutton(end);
	widgets[Pages::GAMEBEGIN]->addbutton(charts);

	while (true)
	{
		BeginBatchDraw();
		widgets[Pages::GAMEBEGIN]->draw();

		if (peekmessage(&msg)) // 检查是否有消息
		{
			int mouseX = msg.x; // 获取鼠标x坐标
			int mouseY = msg.y; // 获取鼠标y坐标

			if (msg.message == WM_LBUTTONDOWN)
			{
				auto click = widgets[Pages::GAMEBEGIN]->mouseclick(mouseX, mouseY);
				if (click != Pages::PAGESNULL && click != Pages::GAMEBEGIN)
				{
					userchoice = click;
					flushmessage();
					break;
				}
			}
			else if (msg.message == WM_MOUSEMOVE)
			{
				widgets[Pages::GAMEBEGIN]->mousemove(mouseX, mouseY);// 处理鼠标移动事件
				widgets[Pages::GAMEBEGIN]->draw();
			}
		}
		flushmessage();
		EndBatchDraw();
	}
}

void Gamestart::gamestart()
{
	//Button* back = new Button(0, 0, 100, 30, "返回", Pages::GAMEBEGIN);
	//widgets[Pages::GAMESTART]->addbutton(back);
	widgets[Pages::GAMESTART]->draw();
	this->game->play();
	flushmessage();
	userchoice = Pages::GAMEBEGIN;
}

void Gamestart::gameend()
{
       exit(0);
}

void Gamestart::drawing(int x, int y, int m_x, int m_y, string line) {
	settextstyle(20, 20, ("微软雅黑"));
	if (num1 >= 0 && num1 <= 2) {
		settextcolor(RGB(247, 255, 0));
		setfillcolor(RGB(101, 31, 235));
	}
	else {
		settextcolor(RGB(115, 43, 245));
		setfillcolor(RGB(220, 207, 247));
	}
	setbkmode(TRANSPARENT);
	int hSpace = (m_x - (line.size()) / 4) / 2 + 20;
	int vSpace = m_y + 5;
	solidroundrect(x, y, m_x, m_y, 10, 10);//无边框填充矩形
	outtextxy(hSpace, vSpace, line.c_str());
}

void Gamestart::thecharts()
{
	Button* back = new Button(0, 0, 100, 30, "返回", Pages::GAMEBEGIN);
	widgets[Pages::GAMECHARTS]->addbutton(back);



	while (true)
	{
		BeginBatchDraw();
		widgets[Pages::GAMECHARTS]->draw();

		std::ifstream Read;
		Read.open("thecharts.txt", ios::in);
		if (!Read.is_open()) {
			Button* fill = new Button(95, 70, 190, 80, "加载失败", Pages::GAMEBEGIN);
			widgets[Pages::GAMECHARTS]->addbutton(fill);
		}
		else {
			std::string line;
			while (std::getline(Read, line)) {
				drawing(100, (140 + num1 * 40), 300, (110 + num1 * 40), line.c_str());
				num1++;
			}
			num1 = 0;
			Read.close();
		}

		if (peekmessage(&msg)) // 检查是否有消息
		{

			int mouseX = msg.x; // 获取鼠标x坐标
			int mouseY = msg.y; // 获取鼠标y坐标

			if (msg.message == WM_LBUTTONDOWN)
			{
				auto click = widgets[Pages::GAMECHARTS]->mouseclick(mouseX, mouseY);
				if (click != Pages::PAGESNULL)
				{
					userchoice = click;
					flushmessage();
					break;
				}
			}
			else if (msg.message == WM_MOUSEMOVE)
			{
				widgets[Pages::GAMECHARTS]->mousemove(mouseX, mouseY);// 处理鼠标移动事件
				widgets[Pages::GAMECHARTS]->draw();

				std::ifstream Read;
				Read.open("thecharts.txt", ios::in);
				if (!Read.is_open()) {
					Button* fill = new Button(95, 70, 190, 80, "加载失败", Pages::GAMEBEGIN);
					widgets[Pages::GAMECHARTS]->addbutton(fill);
				}
				else {
					std::string line;
					while (std::getline(Read, line)) {
						drawing(100, (140 + num1 * 40), 300, (110 + num1 * 40), line.c_str());
						num1++;
					}
					num1 = 0;
					Read.close();
				}
			}
			flushmessage();
			EndBatchDraw();
		}

	}
	//std::ifstream ifs;
	//ifs.open(FILENAME, std::ios::in);//示例，最后改FILENAME
	//unsigned long long score[5] = { 0 };
	//for (int i = 0; i < 5; ++i)
	//{
	//	ifs >> score[i];
	//}
	//ifs.close();
	//outtextxy(0, 0, std::to_string(score[0]).c_str());
	//outtextxy(0, 0, std::to_string(score[1]).c_str());
	//outtextxy(0, 0, std::to_string(score[2]).c_str());
	//outtextxy(0, 0, std::to_string(score[3]).c_str());
	//outtextxy(0, 0, std::to_string(score[4]).c_str());

}

void SetVolume(int volume) {
	// 将音量设置在0到1000之间
	std::string command = "setaudio mp3 volume to " + std::to_string(volume);
	mciSendString(command.c_str(), NULL, 0, NULL);
}

void Gamestart::gameset()
{
	Button* back = new Button(0, 0, 100, 30, "返回", Pages::GAMEBEGIN);
	Button* add = new Button(125, 200, 150, 50, "增大音量", Pages::GAMESET);
	Button* reduce = new Button(125, 300, 150, 50, "减小音量", Pages::GAMESET);
	widgets[Pages::GAMESET]->addbutton(back);
	widgets[Pages::GAMESET]->addbutton(add);
	widgets[Pages::GAMESET]->addbutton(reduce);

	while (true)
	{
		BeginBatchDraw();
		widgets[Pages::GAMESET]->draw();

		if (peekmessage(&msg)) // 检查是否有消息
		{
			int mouseX = msg.x; // 获取鼠标x坐标
			int mouseY = msg.y; // 获取鼠标y坐标

			if (msg.message == WM_LBUTTONDOWN)
			{
				auto click = widgets[Pages::GAMESET]->mouseclick(mouseX, mouseY);
				if (click != Pages::PAGESNULL && click != Pages::GAMESET)
				{
					userchoice = click;
					flushmessage();
					break;
				}
				else if (click == Pages::GAMESET)
				{
					if (add->checkClick(msg.x, msg.y))
					{
						if (this->vol + 100 <= 1000)
						{
							this->vol += 100;
						}
						else
						{
							this->vol = 1000;
						}
					}
					if (reduce->checkClick(msg.x, msg.y))
					{
						if (this->vol - 100 >= 0)
						{
							this->vol -= 100;
						}
						else
						{
							this->vol = 0;
						}
					}
				}
			}
			else if (msg.message == WM_MOUSEMOVE)
			{
				widgets[Pages::GAMESET]->mousemove(mouseX, mouseY);// 处理鼠标移动事件
				widgets[Pages::GAMESET]->draw();
			}
			flushmessage();
			EndBatchDraw();
		}

	}

}

void Gamestart::bgm()
{
	std::string command = "open ./飞机大战素材/音乐素材/BGM.mp3 alias mp3";
	mciSendString(command.c_str(), NULL, 0, NULL);
	mciSendString("play mp3 repeat", NULL, 0, NULL);
	std::string str = "setaudio mp3 volume to ";
	while (true)
	{
		std::string temp = std::to_string(vol);
		Sleep(100);
		if (temp != std::to_string(vol))
		{
			std::string res = str + std::to_string(vol);
			MCIERROR ret = mciSendString(res.c_str(), 0, 0, 0);
			if (ret != 0)
			{
				char err[1024] = { 0 };
				mciGetErrorString(ret, err, sizeof(err));
				std::cout << err << std::endl;
			}
			std::cout << res << std::endl;
		}
	}
}
